<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>gcdiagnostics.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_debug_gcdiagnostics.js.html">gcdiagnostics.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of the GcDiagnostics class.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.debug.GcDiagnostics&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line23"></a>goog.require(&#39;goog.debug.Trace&#39;);
<a name="line24"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Class used for singleton goog.debug.GcDiagnostics.  Used to hook into
<a name="line30"></a> * the L2 ActiveX controller to profile garbage collection information in IE.
<a name="line31"></a> * Can be used in combination with tracers (goog.debug.Trace), to provide object
<a name="line32"></a> * allocation counts from within the tracers or used alone by invoking start and
<a name="line33"></a> * stop.
<a name="line34"></a> *
<a name="line35"></a> * See http://go/l2binary for the install.
<a name="line36"></a> * TODO(user): Move the L2 installer somewhere more general.
<a name="line37"></a> * @constructor
<a name="line38"></a> * @private
<a name="line39"></a> */
<a name="line40"></a>goog.debug.GcDiagnostics_ = function() {};
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>/**
<a name="line44"></a> * Install the GcDiagnostics tool.
<a name="line45"></a> */
<a name="line46"></a>goog.debug.GcDiagnostics_.prototype.install = function() {
<a name="line47"></a>  if (goog.userAgent.IE) {
<a name="line48"></a>    /** @preserveTry */
<a name="line49"></a>    try {
<a name="line50"></a>      var l2Helper = new ActiveXObject(&#39;L2.NativeHelper&#39;);
<a name="line51"></a>
<a name="line52"></a>      // If using tracers, use the higher precision timer provided by L2.
<a name="line53"></a>      if (goog.debug.Trace_) {
<a name="line54"></a>        goog.debug.Trace_.now = function() {
<a name="line55"></a>          return l2Helper[&#39;getMilliSeconds&#39;]();
<a name="line56"></a>        };
<a name="line57"></a>      }
<a name="line58"></a>
<a name="line59"></a>      if (l2Helper[&#39;gcTracer&#39;]) {
<a name="line60"></a>        l2Helper[&#39;gcTracer&#39;][&#39;installGcTracing&#39;]();
<a name="line61"></a>        this.gcTracer_ = l2Helper[&#39;gcTracer&#39;];
<a name="line62"></a>        if (goog.debug.Trace) {
<a name="line63"></a>          // If tracers are in use, register the gcTracer so that per tracer
<a name="line64"></a>          // allocations are recorded.
<a name="line65"></a>          goog.debug.Trace.setGcTracer(this.gcTracer_);
<a name="line66"></a>        }
<a name="line67"></a>      }
<a name="line68"></a>      this.logger_.info(&#39;Installed L2 native helper&#39;);
<a name="line69"></a>    } catch (e) {
<a name="line70"></a>      this.logger_.info(&#39;Failed to install L2 native helper: &#39; + e);
<a name="line71"></a>    }
<a name="line72"></a>  }
<a name="line73"></a>};
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * Logger for the gcDiagnotics
<a name="line78"></a> * @type {goog.debug.Logger}
<a name="line79"></a> * @private
<a name="line80"></a> */
<a name="line81"></a>goog.debug.GcDiagnostics_.prototype.logger_ =
<a name="line82"></a>    goog.debug.Logger.getLogger(&#39;goog.debug.GcDiagnostics&#39;);
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Starts recording garbage collection information.  If a trace is already in
<a name="line87"></a> * progress, it is ended.
<a name="line88"></a> */
<a name="line89"></a>goog.debug.GcDiagnostics_.prototype.start = function() {
<a name="line90"></a>  if (this.gcTracer_) {
<a name="line91"></a>    if (this.gcTracer_[&#39;isTracing&#39;]()) {
<a name="line92"></a>      this.gcTracer_[&#39;endGcTracing&#39;]();
<a name="line93"></a>    }
<a name="line94"></a>    this.gcTracer_[&#39;startGcTracing&#39;]();
<a name="line95"></a>  }
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Stops recording garbage collection information.  Logs details on the garbage
<a name="line101"></a> * collections that occurred between start and stop.  If tracers are in use,
<a name="line102"></a> * adds comments where each GC occurs.
<a name="line103"></a> */
<a name="line104"></a>goog.debug.GcDiagnostics_.prototype.stop = function() {
<a name="line105"></a>  if (this.gcTracer_ &amp;&amp; this.gcTracer_[&#39;isTracing&#39;]()) {
<a name="line106"></a>    var gcTracer = this.gcTracer_;
<a name="line107"></a>    this.gcTracer_[&#39;endGcTracing&#39;]();
<a name="line108"></a>
<a name="line109"></a>    var numGCs = gcTracer[&#39;getNumTraces&#39;]();
<a name="line110"></a>    this.logger_.info(&#39;*********GC TRACE*********&#39;);
<a name="line111"></a>    this.logger_.info(&#39;GC ran &#39; + numGCs + &#39; times.&#39;);
<a name="line112"></a>    var totalTime = 0;
<a name="line113"></a>    for (var i = 0; i &lt; numGCs; i++) {
<a name="line114"></a>      var trace = gcTracer[&#39;getTrace&#39;](i);
<a name="line115"></a>
<a name="line116"></a>      var msStart = trace[&#39;gcStartTime&#39;];
<a name="line117"></a>      var msElapsed = trace[&#39;gcElapsedTime&#39;];
<a name="line118"></a>
<a name="line119"></a>      var msRounded = Math.round(msElapsed * 10) / 10;
<a name="line120"></a>      var s = &#39;GC &#39; + i + &#39;: &#39; + msRounded + &#39; ms, &#39; +
<a name="line121"></a>          &#39;numVValAlloc=&#39; + trace[&#39;numVValAlloc&#39;] + &#39;, &#39; +
<a name="line122"></a>          &#39;numVarAlloc=&#39; + trace[&#39;numVarAlloc&#39;] + &#39;, &#39; +
<a name="line123"></a>          &#39;numBytesSysAlloc=&#39; + trace[&#39;numBytesSysAlloc&#39;];
<a name="line124"></a>      if (goog.debug.Trace) {
<a name="line125"></a>        goog.debug.Trace.addComment(s, null, msStart);
<a name="line126"></a>      }
<a name="line127"></a>      this.logger_.info(s);
<a name="line128"></a>      totalTime += msElapsed;
<a name="line129"></a>    }
<a name="line130"></a>    if (goog.debug.Trace) {
<a name="line131"></a>      goog.debug.Trace.addComment(&#39;Total GC time was &#39; + totalTime + &#39; ms.&#39;);
<a name="line132"></a>    }
<a name="line133"></a>    this.logger_.info(&#39;Total GC time was &#39; + totalTime + &#39; ms.&#39;);
<a name="line134"></a>    this.logger_.info(&#39;*********GC TRACE*********&#39;);
<a name="line135"></a>  }
<a name="line136"></a>};
<a name="line137"></a>
<a name="line138"></a>
<a name="line139"></a>/**
<a name="line140"></a> * Singleton GcDiagnostics object
<a name="line141"></a> * @type {goog.debug.GcDiagnostics_}
<a name="line142"></a> */
<a name="line143"></a>goog.debug.GcDiagnostics = new goog.debug.GcDiagnostics_();
</pre>


</body>
</html>
